cmake_minimum_required(VERSION 3.10)
project(chart_plugin)

set(CC_OPTIMIZATION_LEVEL "-O1")
set(CC_OVERALL_FLAGS "-pipe")
set(CC_WARNING_FLAGS "-Wall -Wtrampolines -Wdate-time -Wformat=1 -Werror=format-security")
set(CXX_GENERATION_FLAGS "-fno-common -freg-struct-return")
set(CXX_OPTIMIZATION_FLAGS "")
set(CC_SECURITY_FLAGS "-fPIC -fPIE -fstack-protector-strong")
set(CXX_DEFINE_FLAGS "")
set(CXX_ALL_OPTIONS "${CC_OPTIMIZATION_LEVEL} ${CC_OVERALL_FLAGS} ${CC_WARNING_FLAGS} ${CXX_GENERATION_FLAGS} ${CXX_OPTIMIZATION_FLAGS} ${CC_SECURITY_FLAGS} ${CXX_DEFINE_FLAGS}")

set(CC_LANGUAGE_FLAGS "-fsigned-char")
set(C_ALL_OPTIONS "${CC_OPTIMIZATION_LEVEL} ${CC_OVERALL_FLAGS} ${CC_WARNING_FLAGS} ${CC_LANGUAGE_FLAGS} ${CC_SECURITY_FLAGS}")
set(SAFE_LINK_OPTION "-pie -Wl,-z,relro -Wl,-z,noexecstack -Wl,-z,now")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--disable-new-dtags,-z,relro,-z,now,-z,noexecstack -s")
set(CMAKE_SKIP_RPATH TRUE)
set(CMAKE_CXX_STANDARD 14)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CXX_ALL_OPTIONS} -fno-sized-deallocation")

add_custom_target(CustomMsgDependency)

set(ROS_VERSION "$ENV{ROS_DISTRO}")
if("$ENV{ROS_DISTRO}" STREQUAL "noetic")
    add_compile_definitions(NOETIC)
endif()

set(CMAKE_PREFIX_PATH "${CMAKE_PREFIX_PATH};/opt/ros/${ROS_VERSION};/usr/lib/x86_64-linux-gnu/cmake/Qt5;${CMAKE_BINARY_DIR}/third_party/sysroot/opt/ros/${ROS_VERSION};${CMAKE_BINARY_DIR}/third_party/qt/5.9.5/gcc_64/lib/cmake/Qt5")

if (EXISTS ${CMAKE_BINARY_DIR}/third_party/sysroot/usr/include/OGRE/RenderSystems/GL/)
    set(_qt5gui_OPENGL_INCLUDE_DIR "${CMAKE_BINARY_DIR}/third_party/sysroot/usr/include/OGRE/RenderSystems/GL")
endif ()

find_package(catkin REQUIRED COMPONENTS roscpp rviz std_msgs)

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_BUILD_TYPE "Release")
find_package(Qt5 REQUIRED COMPONENTS Core Widgets Gui)
set(QT_LIBRARIES Qt5::Widgets)

catkin_package(CATKIN_DEPENDS roscpp rviz std_msgs)

set(SOURCE_FILES
    ${CMAKE_CURRENT_SOURCE_DIR}/src/st_space_display.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/src/st_space/st_space_data.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/src/st_space/ui_st_widget.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/src/curve_display.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/src/curve/curve_data.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/src/curve/ui_curve_widget.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/src/common/utils.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/src/common/chart_display_base.h
    ${CMAKE_CURRENT_SOURCE_DIR}/src/slt_space_display.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/src/slt_space/slt_space_data.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/src/slt_space/slt_space_draw.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/src/slt_space/ui_slt_widget.cpp)

link_directories(
    ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/qwt-6.1.4/qwt-6.1.4/build/lib/
    ${catkin_LIBRARY_DIRS}
    ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/mathgl-2.4.4/mathgl-2.4.4/build/lib/
    ${CMAKE_BINARY_DIR}/third_party/sysroot/opt/ros/${ROS_VERSION}/lib)

add_library(${PROJECT_NAME} ${SOURCE_FILES} ${CMAKE_CURRENT_SOURCE_DIR}/resource/res.qrc)
add_dependencies(${PROJECT_NAME} CustomMsgDependency)
set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS ${SAFE_LINK_OPTION})

target_link_libraries(${PROJECT_NAME} ${QT_LIBRARIES} ${catkin_LIBRARIES} mgl mgl-qt5 qwt)

target_include_directories(${PROJECT_NAME}
    PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/qwt-6.1.4/qwt-6.1.4/build/include/
    ${catkin_INCLUDE_DIRS}
    ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/mathgl-2.4.4/mathgl-2.4.4/build/include/
    ${CMAKE_BINARY_DIR}/../devel/include/
    ${CMAKE_BINARY_DIR}/third_party/sysroot/opt/ros/${ROS_VERSION}/include
    ${CMAKE_BINARY_DIR}/third_party/sysroot/opt/ros/${ROS_VERSION}/include/rviz
    ${CMAKE_BINARY_DIR}/third_party/sysroot/usr/include/OGRE
    ${CMAKE_BINARY_DIR}/third_party/class_loader/include
    ${CMAKE_BINARY_DIR}/third_party/console_bridge/include
    ${CMAKE_BINARY_DIR}/third_party/Poco/include)
